//index.js

// let {GetUserInfo,GetUserRecipe,GetOpenid}=require("../../api/api");
let {GetUserRecipe}=require("../../api/api");

Page({
  data: {
    viewShowed: true, //控制授权能否显示
    recipesInfo:{}, //食谱数据 包含历史食谱 RecordData,
    loadingShow:true,
    //名字
    title:"",
    //日期
    date:"",
    info:"恭喜您开启B30瘦身之旅，接下来会由营养师全程一对一陪伴您完成减重。我们将通过陪伴、调整、帮助等方式让您形成健康的生活方式，科学的管理体重。以下是为您私人订制的减重食谱，如有任何疑问请及时咨询营养师，祝您早日瘦身成功！",
    dietContentData:[],
    //注意事项
    announcements:"",
    // 饮食互换
    suggestTitles:["肉类建议","蔬菜建议","水果建议"],
    suggestActive:0,

    suggestTabsContent:[
      {
        type:'肉类',
        suggestImg:"../../image/meat.png",
        suggest:'鱼虾贝类、鸡胸肉、去皮鸡腿、牛羊瘦肉、猪瘦肉。',
        tabooImg:"../../image/meatTaboo.png",
        taboo:'肥肉、腌肉、五花肉、动物皮（包括爪子翅膀、鸡皮鱼皮）、内脏、卤制肉类、熏烤类、各种加工肉类。'
      },
      {
        type:'蔬菜',
        suggestImg:"../../image/vegetable.png",
        suggest:'卷心菜、西兰花、菠菜、菜心、海带、黑木耳、冬瓜、丝瓜、白萝卜、秋葵。',
        tabooImg:"../../image/vegetableTaboo.png",
        taboo:'土豆、芋头、老南瓜、山药、莲藕、茄子。'
      },
      {
        type:'水果',
        suggestImg:"../../image/fruits.png",
        suggest:'圣女果、苹果、猕猴桃、桃子、李子、杏、雪莲果、火龙果、番石榴、橙子、柚子、樱桃、菠萝等。',
        tabooImg:"../../image/fruitsTaboo.png",
        taboo:'香蕉、榴莲、龙眼、荔枝、大枣、柿子、菠萝蜜、山竹等。'
      }
    ],
 
    heightVal:0,
    isShowMeals:[false,false,false,false],
    dietDates:[
      {
        img:"../../image/recipe/recipe-meals1.jpg",
        title:"早餐",
        date:"07:00-09:00"
      },
      {
        img:"../../image/recipe/recipe-meals4.jpg",
        title:"加餐",
        date:"10:00-10:30"
      },
      {
        img:"../../image/recipe/recipe-meals2.jpg",
        title:"午餐",
        date:"11:00-13:00"
      },
      {
        img:"../../image/recipe/recipe-meals4.jpg",
        title:"加餐",
        date:"15:00-16:00"
      },
      {
        img:"../../image/recipe/recipe-meals3.jpg",
        title:"晚餐",
        date:"17:00-19:00"
      }
    ],
    // 体重管理
    weightManage:[0,0,0,0,0,0,0],
    weightManageCurrent:[0,0,0,0,0,0,0],
    weightManageDate:["周日","周一","周二","周三","周四","周五","周六"],
  //当前体重
  // recipeCurrentValue:"",
  // recipeValue:"",
  isShowrecipe:false,
  popoutText:"",
  popoutValue:0,
  popoutIndex:0,
  weekIndex:0,
  dieticianIndex:0,
  dieticianData:[
    {
      head:"../../image/recipe/recipe-head.jpg",
      info:"陪伴是最长情的告白，我们将尽其所能陪你一起实现完美蜕变！"
    },
    {
      head:"../../image/recipe/recipe-head2.jpg",
      info:"你要悄悄瘦下来，然后惊艳所有人，相信自己，你也可以变得很美！"
    },
    {
      head:"../../image/recipe/recipe-head3.jpg",
      info:"女人最值得炫富的地方，年龄成谜，身材无敌！只要你越瘦运气就会越好！"
    },
    {
      head:"../../image/recipe/recipe-head4.jpg",
      info:"既然总是有人减肥成功，为何不能是你，唯有自律才能助你成功逆袭！"
    },
    {
      head:"../../image/recipe/recipe-head5.jpg",
      info:"减肥成功没有偶然，只有保持一种积极进取的态度，满怀信心地一步一步往前走，最后成就步步高的人生！"
    },
    {
      head:"../../image/recipe/recipe-head6.jpg",
      info:"把懒惰放一边，把丧气的话收一收，把积极性提一提，把矫情的心放一放，才能收获你想要的身材！"
    },
    {
      head:"../../image/recipe/recipe-head7.jpg",
      info:"人生不能靠心情减肥，而要靠心态去减肥，将来的你一定会感谢现在拼命减肥的你！"
    },
    {
      head:"../../image/recipe/recipe-head8.jpg",
      info:"减肥没有那么容易，每块肉都有它的脾气，让我们一起将减肥进行到底！"
    },
    {
      head:"../../image/recipe/recipe-head9.jpg",
      info:"如果连自己的体重都控制不了，怎么能掌控自己的人生呢，你的心事，我们都懂！一起fighting！"
    },
  ]
  },


  // 弹框选项
  showrecipe(e){
    let index=e.currentTarget.dataset.index;
    this.setData({
      // popoutText:this.data.weightManageDate[index],
      // popoutValue:this.data.weightManage[index],
      popoutIndex:index,
      isShowrecipe:true,
    })

  },
  hiddenrecipe(){
    this.setData({
      isShowrecipe:false
    })
  },
  changerecipe(e){
    let {weightManageCurrent,popoutIndex}=this.data;
    const val = e.detail.value;

    weightManageCurrent[popoutIndex]=val;

    this.setData({
      weightManageCurrent
    })
  },
  saverecipe(){
    let {weightManageCurrent,popoutIndex,weightManage}=this.data;
    if(!weightManageCurrent[popoutIndex]){
      wx.showToast({

        title: '值不能为空',
   
        icon: 'none',
   
        duration: 2000//持续的时间
   
      })
      return;
    }
    weightManage[popoutIndex]=weightManageCurrent[popoutIndex];
    wx.setStorageSync("weightManage", weightManage);
    this.setData({
      weightManage
    })
    this.hiddenrecipe();
  },

  //显示三餐内容
  showMeals(e){
    let {isShowMeals}=this.data;
    let index=e.currentTarget.dataset.index;
    isShowMeals[index]=!isShowMeals[index];
    
    this.setData({
      isShowMeals
    })
  },
//饮食互换 切换
  toggleSuggestActive(e){
      this.setData({
        suggestActive:e.currentTarget.dataset.index
      })
  },
  //获取当前营养师头像和语句
  getDieticianInfo(){
    let  randomNum=Math.floor(Math.random()*9);
    let timestamp=Date.parse(new Date());
    let expiration=timestamp+86400000; 
    // console.log(randomNum);
    let randomStorage=wx.getStorageSync('randomStorage');
    if (randomStorage) {
        if (timestamp>randomStorage.expiration) {
              wx.setStorageSync('randomStorage', {randomNum,expiration})
        }
    }else{
        wx.setStorageSync('randomStorage', {randomNum,expiration})
    }
    randomStorage=wx.getStorageSync('randomStorage');
    this.setData({
      dieticianIndex:randomStorage.randomNum
    })
  },  
  // getFirstDayOfWeek (date) {
  //   var day = date.getDay() || 7;
  //   return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - day).getTime();
  // },
  // countDay(time1,time2){
  //   return Math.abs((time1-time2)/(1000*60*60*24))+1;
  // },
  onLoad: function () { 
      this.getDieticianInfo();
  },  
  onShow:function(){





    // 86400000 //一天的毫秒
    // wx.removeStorageSync

    
    // var date = new Date("2021-7-26").getTime();
    // var date2 = new Date("2021-7-30").getTime();
    // console.log(date);
    // console.log(date2);
    // if(week==0){

    // }
    // let date2=this.getFirstDayOfWeek(date);
    // let a=this.countDay(date,date2)
    // console.log(a);
    var week = new Date().getDay(); 
    let headFalg=true;
    if (week==0||week==2||week==4||week==6) {
      headFalg=true;
    }else{
      headFalg=false;
    }
    this.setData({
      weekIndex:week,
      headFalg
    })
   let weightManage= wx.getStorageSync("weightManage");
  if (weightManage) {
    setTimeout(()=>{
      this.setData({
        heightVal:120,
        weightManage
      })
    },100)
  }

    let phone_num=wx.getStorageSync("phone_num");
    console.log(phone_num);
    if(phone_num){
      wx.showLoading(
        {
          title: '加载中',
          mask:"true"
        }
      );
      GetUserRecipe(phone_num,(e)=>{
        wx.hideLoading();
        var recipesData = e.data;
          /*
          CustomerID  客户编码 A9系统
          CustomerName 客户名称 A9系统
          Tel 客户电话 A9系统
          Title  食谱标题
          RecipesDate  日期
          Greetings 问候语
          Reminder  温馨提示
          EatDiet7  早餐（7:00-9:00）
          EatDiet10 加餐（10:00-10:30）
          EatDiet11 午餐（11:00-13:00）
          EatDiet15 加餐（15:00-16:00）
          EatDiet17 晚餐（17:00-19:00）
          DietExchange 饮食互换建议   
          RecordData 客户历史食谱(列表，字段跟上面字段一致)
          */
         console.log(recipesData.DietExchange);
          if(recipesData!='')
          {
            let dietContentData=[];
            dietContentData.push(recipesData.EatDiet7);
            dietContentData.push(recipesData.EatDiet10);
            dietContentData.push(recipesData.EatDiet11);
            dietContentData.push(recipesData.EatDiet15);
            dietContentData.push(recipesData.EatDiet17);
            wx.setStorageSync('CustomerID', recipesData.CustomerID);
            this.setData({
              viewShowed: true,
              recipesInfo: recipesData,
              title:recipesData.Title,
              date:recipesData.RecipesDate,
              // info:recipesData.Greetings,
              dietContentData,
              loadingShow:false,
              announcements:recipesData.DietExchange
            })
          }else{
            this.setData({
              loadingShow:false
            })
          }
      });
    }








    // wx.login({
    //   success: res => {
    //     let code=res.code;
    //    GetOpenid(code,(e)=>{
    //       let openid=e.data.openid;
    //       let session_key=e.data.session_key;
    //       wx.setStorageSync("openid",openid);
    //       wx.setStorageSync("session_key",session_key);
   
    //         GetUserInfo(openid,(e)=>{
    //           var data = e.data;
    //           if(data){
    //             var phone_num = data.phone_num;
    //               if(phone_num!=''){//不为空 根据电话号码获取用户食谱数据
    //                 wx.showLoading();
    //                 GetUserRecipe(phone_num,(e)=>{
    //                   wx.hideLoading();
    //                   var recipesData = e.data;
    //                     /*
    //                     CustomerID  客户编码 A9系统
    //                     CustomerName 客户名称 A9系统
    //                     Tel 客户电话 A9系统
    //                     Title  食谱标题
    //                     RecipesDate  日期
    //                     Greetings 问候语
    //                     Reminder  温馨提示
    //                     EatDiet7  早餐（7:00-9:00）
    //                     EatDiet10 加餐（10:00-10:30）
    //                     EatDiet11 午餐（11:00-13:00）
    //                     EatDiet15 加餐（15:00-16:00）
    //                     EatDiet17 晚餐（17:00-19:00）
    //                     DietExchange 饮食互换建议   
    //                     RecordData 客户历史食谱(列表，字段跟上面字段一致)
    //                     */
    //                   //  console.log(recipesData);
    //                     if(recipesData!='')
    //                     {
    //                       let dietContentData=[];
    //                       dietContentData.push(recipesData.EatDiet7);
    //                       dietContentData.push(recipesData.EatDiet10);
    //                       dietContentData.push(recipesData.EatDiet11);
    //                       dietContentData.push(recipesData.EatDiet15);
    //                       dietContentData.push(recipesData.EatDiet17);
    //                       wx.setStorageSync('CustomerID', recipesData.CustomerID);
    //                       this.setData({
    //                         viewShowed: true,
    //                         recipesInfo: recipesData,
    //                         title:recipesData.Title,
    //                         date:recipesData.RecipesDate,
    //                         // info:recipesData.Greetings,
    //                         dietContentData,
    //                         loadingShow:false,
    //                         announcements:recipesData.DietExchange
    //                       })
    //                     }else{
    //                       this.setData({
    //                         loadingShow:false
    //                       })
    //                     }
    //                 });
    //               }else{

    //                 wx.showModal({
    //                   title: '登录才能获取专属饮食方案',
    //                   content: '是否跳转到登录授权页面',
    //                   success (res) {
    //                     if (res.confirm) {
    //                       console.log('用户点击确定');
    //                       wx.reLaunch({
    //                         url: '../login/login',
    //                       })
    //                     } else if (res.cancel) {
    //                       console.log('用户点击取消')
    //                     }
    //                   }
    //                 })
                 
    //               }
    //           }
    //           else{ 
    //             wx.showModal({
    //               title: '登录才能获取专属饮食方案',
    //               content: '是否跳转到登录授权页面',
    //               success (res) {
    //                 if (res.confirm) {
    //                   console.log('用户点击确定');
    //                   wx.reLaunch({
    //                     url: '../login/login',
    //                   })
    //                 } else if (res.cancel) {
    //                   console.log('用户点击取消')
    //                 }
    //               }
    //             })
           
    //           }
    //         })
    //    })
    //   }
    // })
   }
})
